/* 
 * File:   Polygon.cpp
 * Author: lgr013
 * 
 * Created on 13 de septiembre de 2014, 14:57
 */

#include "Polygon.h"

Polygon::Polygon() {
}

Polygon::Polygon(ObjFactory *objFactory, bool flagStatic, float posX, float posY, int scale, int sides, float mass, float rotation, int idImage) {
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);

    if (scale <= 0) {
        myLogFile->writeInLog(2, "[2] El radio asociado al poligono es invalido (Radio no positivo)\n");
    }

    Dot *dot = new Dot(posX, posY, 0, 0); // ancho y alto = 0  
    b2Vec2 size(0, 0);
    float sca = scale * 0.01;
    Object *object = objFactory->getObject(flagStatic, dot->dotPixelsToWorld(), &size, sca, mass, sides, rotation);
    this->object = object;

    struct ObjectInit auxData;
    auxData.estatico = flagStatic;

    if (sides == 3) {
        auxData.objectId = TRIANGLE;
    } else if (sides == 5) {
        auxData.objectId = PENTAGON;
    } else if (sides == 6) {
        auxData.objectId = HEXAGON;
    }

    auxData.imageId = idImage;
    auxData.x = posX;
    auxData.y = posY;
    auxData.height = 2 * scale;
    auxData.width = 2 * scale;
    auxData.rot = rotation;

    auxData.idCharacter = VACIO;
    auxData.stateCharacter = VACIO;

    this->data = auxData;

    myLogFile->writeInLog(0, "[0] Se ha creado un poligono exitosamente\n");

    this->equals = false;
    delete dot;
}

void Polygon::restartPlayer(){}

void Polygon::updateData() {
    this->updatePosition();
    this->updateRotation();
}

void Polygon::checkChanges(){
    b2Vec2 pos = this->object->getPosition();
    if (this->data.x == pos.x && this->data.y == pos.y && this->data.rot  == this->object->getAngle()){
        this->equals = true;        
    }else{
        this->equals = false;        
    }

}

void Polygon::updateRotation() {
    this->data.rot = this->object->getAngle();
}

void Polygon::updatePosition() {
    b2Vec2 pos = this->object->getPosition();
    this->data.x = pos.x;
    this->data.y = pos.y;

}

struct ObjectInit Polygon::getData() {
    this->updateData();
    return this->data;
}

b2Vec2 Polygon::getPosition() {
    return this->object->getPosition();
}

float Polygon::getAngle() {
    return this->object->getAngle();
}

float Polygon::getAngularVelocity() {
    return this->object->getAngularVelocity();
}

void Polygon::setAngularVelocity(float velocity) {
    this->object->setAngularVelocity(velocity);
}

void Polygon::setUbicDyn(int id){
    this->data.ubicDyn = id;
}

bool Polygon::getAreEquals(){
    return this->equals;
}
b2Body* Polygon::getBody() {
    return this->object->getBody();
    
}
Polygon::~Polygon() {
    delete this->object;
//    printf("destructor Polygon \n");
}

